package sz.szsmk.citizencard.ui.newversion.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chenenyu.router.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.android.synthetic.main.activity_accountsecurity.*
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity
import sz.szsmk.citizencard.ui.dzsmkavtivity.GestureLoginActivity
import sz.szsmk.citizencard.ui.newversion.config.Constants
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.JumpUtils

/**
 * author : hyt
 * date : 2020/5/20
 * description :
 */
@Route(Constants.ACCOUNTSECURITYACTIVITY)
class AccountSecurityActivity : AppBaseActivity() {

    override fun getTopTitle() = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_accountsecurity)

        psdadmin_reset_login.setOnClickListener {
            onClick(it)
        }

        immersionBar {
            fitsSystemWindows(true)
            statusBarDarkFont(false,0.2f)
            statusBarColor(R.color.color_2184FA)
        }
        back!!.setOnClickListener { v: View? -> finish() }

//        initView()
    }

    override fun onResume() {
        super.onResume()
        //判断手势  未创建手势- 创建后 返回当前页面    获取isOpen  判断 是否开启
        if (Constant.isOpen == "1") {
            tv_status!!.text = "已开启"
            tv_status!!.setTextColor(ContextCompat.getColor(this, R.color.color_00AAFF))
        } else {
            tv_status!!.text = "未开启"
            tv_status!!.setTextColor(ContextCompat.getColor(this, R.color.color_637077))
        }
    }

    private fun initView() {
        //判断指纹

        if(ACache.get(DaggerApplication.getIns()).getAsObject("fingerprint") !=null){
            val bo = ACache.get(DaggerApplication.getIns()).getAsObject("fingerprint")  as Boolean
            sw_fingerprint!!.isChecked = ACache.get(DaggerApplication.getIns()).getAsObject("fingerprint") != null && bo
        }else{
            sw_fingerprint!!.isChecked = false
        }
        sw_fingerprint!!.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean -> ACache.get(DaggerApplication.getIns()).put("fingerprint", isChecked) }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null) {
            if (requestCode == 100 && data.getIntExtra("code", 0) == MessageEvent.CODE_GESTURESUCCESS) {
                //手势验证成功
                if (Constant.isOpen == "1") {
                    tv_status!!.text = "已开启"
                    tv_status!!.setTextColor(ContextCompat.getColor(this, R.color.color_00AAFF))
                } else {
                    tv_status!!.text = "未开启"
                    tv_status!!.setTextColor(ContextCompat.getColor(this, R.color.color_637077))
                }
            }
        }
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.psdadmin_reset_login ->                 //0.不存在 1.存在已开启 2.存在未开启
                if (Constant.isOpen == "0") {
                    JumpUtils.jump(this, "gesPasswordSettingActivity", 0)
                } else {
                    // 再调用一次  就是关闭
                    val bundle = Bundle()
                    bundle.putBoolean("isNeedVaild", true)
                    bundle.putString("url", "accountSecurity")
                    val intent = Intent(this@AccountSecurityActivity, GestureLoginActivity::class.java)
                    intent.putExtras(bundle)
                    startActivityForResult(intent, 100)
                }
        }
    }
}